package com.meirengu.channel.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.meirengu.utils.Base64Utils;
import com.meirengu.utils.EncodeUtil;
import com.meirengu.utils.HttpClientInvoker;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


public class testFront {

    public static void main(String[] args) {
        String frontUrl = "http://116.236.184.237:8851/front/HttpForwardProcessor";
//		 String frontUrl =  "http://192.168.13.15:8077/front/HttpForwardProcessor";
//		String frontUrl =  "http://127.0.0.1:8080/front/HttpForwardProcessor";
        String url = null;
        String params = null;
        BASE64Encoder base64Encoder = new BASE64Encoder();
        BASE64Decoder base64Decoder = new BASE64Decoder();

        try {

            url = Base64Utils.encodeBuffer("http://192.168.13.27:8888/flexsupt/flex/msg/ImportList0utService.action", "utf-8");

//			url = Base64Utils.encodeBuffer("http://127.0.0.1:8080/flexsupt/flex/msg/ImportList0utService.action","utf-8");

            byte[] key = Base64Utils.base64ToByteArray("yhdjZGfcsd56fdtsbW5vcHFyc3R1ldx4".trim());
			 /*String sendStr = "{'supplierCode':'01','importIndex':152635,'importList':["
						+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024022','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'01','marriage':'10','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024012','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'01','marriage':'20','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024013','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'02','marriage':'10','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024014','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'01','marriage':'30','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024005','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'02','marriage':'10','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024016','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'01','marriage':'10','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024017','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'01','marriage':'10','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024018','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'01','marriage':'40','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024019','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'02','marriage':'10','eduExperience':'5','applyTerm':'6','applySum':'1000'},"
				 		+ "{'rowIndex':null,'supplierCodeSub':'MKT-DOO-2199799','name':'李四','phone':'15521024020','cityCode':'1111','professionType':'1','incomeRange':'4','incomeType':'1','isCreditRecord':'1','isWelfare':'1','isHousingFund':'1','isHasCar':'1','isCarLoan':'1','isHasHouse':'1','isInsurance':'1','flowNo':'PA0100','custSex':'01','marriage':'40','eduExperience':'5','applyTerm':'6','applySum':'1000'}"
			 		+ "]}";*/
            /* */
            String sendStr = "{'supplierCode':'38','importIndex':'81673de9f01011e7b92700163e0c24a8','importList':["
                    + "{'supplierCodeSub':null,'name':'黎辉明', 'phone':'13558300206','birthday':'1989-06-06','incoming':'20000',"
                    + "'cardType':null,'cardNo':null,'cityCode':'1318','professionType':'2','incomeRange':'1','incomeType':'2',"
                    + "'isCreditRecord':'1','isWelfare':'1','isInsurance':'2','isHousingFund':'1','isHasCar':'2','isCarLoan':null,'isHasHouse':'1','custSex':'01','applyTerm':'12','applySum':'20000'}]}";


//			{"importIndex":"81673de9f01011e7b92700163e0c24a8","importList":[{"applySum":"20000",
//					"birthday":"1989-06-06","cityCode":"2645","custSex":"01","incomeRange":"1",
//					"incomeType":"2","incoming":20000,"isCreditRecord":"1","isHasCar":"2",
//					"isHasHouse":"1","isHousingFund":"1","isInsurance":"2","isWelfare":"1",
//					"name":"黎辉明","phone":"188th23005599","professionType":"2"}],"supplierCode":"38"}

//			{"importIndex":"7f6170dcf02911e7b92700163e0c24a8","importList":[{"applySum":"20000",
//					"birthday":"1981-09-21","cityCode":"1318","custSex":"01","incomeRange":"1",
//					"incomeType":"2","incoming":20000,"isCarLoan":"1","isCreditRecord":"1",
//					"isHasCar":"1","isHasHouse":"1","isHousingFund":"2","isInsurance":"1",
//					"isWelfare":"1","name":"曾慰","phone":"13558300206","professionType":"2"}],
//				"supplierCode":"38"}
			 /*String sendStr = "{'supplierCode':'16','importIndex':'null','importList':["
			 		+ "{'supplierCodeSub':'MKT-YUWEN-2016','name':'韩晓宁', 'phone':'13891380707','birthday':'1981-07-07','incoming':'3500',"
			 		+ "'cardType':'01','cardNo':'610581198107072537','cityCode':'3008','professionType':'3','incomeRange':'3','incomeType':'1',"
			 		+ "'isCreditRecord':'1','isWelfare':'1','isHasCar':'1','isCarLoan':'2','isHasHouse':'2','custSex':'1','applyTerm':'12','applySum':'10000'},"
			 		+ "{'supplierCodeSub':'MKT-YUWEN-2016','name':'周有森', 'phone':'13907618999','birthday':'1962-10-25','incoming':'8000',"
			 		+ "'cardType':'01','cardNo':'460001196210250737','cityCode':'1475','professionType':'3','incomeRange':'2','incomeType':'1',"
			 		+ "'isCreditRecord':'1','isWelfare':'1','isHasCar':'2','isCarLoan':'2','isHasHouse':'2','custSex':'1','applyTerm':'24','applySum':'100000'},"
			 		+ "{'supplierCodeSub':'MKT-YUWEN-2016','name':'吴江', 'phone':'13431224142','birthday':'1991-11-09','incoming':'5000',"
			 		+ "'cardType':'01','cardNo':'430626199111094810','cityCode':'2665','professionType':'3','incomeRange':'2','incomeType':'1',"
			 		+ "'isCreditRecord':'1','isWelfare':'1','isHasCar':'2','isCarLoan':'2','isHasHouse':'2','custSex':'1','applyTerm':'12','applySum':'10000'},"
			 		+ "{'supplierCodeSub':'MKT-YUWEN-2016','name':'张海滨', 'phone':'18926638882','birthday':'1972-01-21','incoming':'100000',"
			 		+ "'cardType':'01','cardNo':'410822197201210515','cityCode':'2703','professionType':'3','incomeRange':'1','incomeType':'1',"
			 		+ "'isCreditRecord':'1','isWelfare':'1','isHasCar':'1','isCarLoan':'2','isHasHouse':'2','custSex':'1','applyTerm':'6','applySum':'200000'}]}";*/
//			String sendStr = "{\"supplierCode\":\"01\",\"importIndex\":152634,\"importList\":[{\"rowIndex\":1,\"supplierCodeSub\":\"MKT-DOO-2199799\",\"name\":\"张三\",\"phone\":\"15121025064\",\"birthday\":null,\"incoming\":0,\"cardType\":null,\"cardNo\":null,\"cityCode\":\"1111\"}]}";

            byte[] sendByte = EncodeUtil.des3EncodeECB(key, sendStr.getBytes("utf-8"));

            String encryptStr = base64Encoder.encodeBuffer(sendByte);
            System.out.println(encryptStr);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("sendData", encryptStr);
            System.out.println(Base64Utils.encodeBuffer(jsonObject.toString(), "utf-8"));

            Map<String, Object> requestParams = new HashMap<String, Object>();
            requestParams.put("params", Base64Utils.encodeBuffer(jsonObject.toString(), "utf-8"));
            requestParams.put("url", url);
            requestParams.put("method", "post");
            requestParams.put("posttype", "HTTPCLIENT");//在前置添加HTTPCLIENT转发方式
            requestParams.put("charcode", "utf-8");

            HttpClientInvoker httpInvoker = HttpClientInvoker.getInstance();
            String callRst = httpInvoker.invoke(frontUrl, requestParams, true);//使用前置转发
//		    System.out.println(callRst);
//			byte[] callRstBytes =Base64Utils.decodeBuffer(callRst);
            String returnStr = Base64Utils.decodeBuffer(callRst, "utf-8");

            /**
             * 解外层包，把returnData解压出来
             */
            JSONObject returnObject = new JSONObject();
            returnObject = JSON.parseObject(returnStr);
            String payload = returnObject.get("returnData").toString();

//			String payload = "VS6uakm143dn2L8OwhDBj6ZWwqHRcUC5U8SDnjQn8yWQRtfO0Fa41zYaJaBoeuQYofbRkWJFWiCB\nsmJ9WPG8UPTXb8is4GbkmBB+NMZ5mKgohvVOyWMbzv65w0zHR45CKIb1TsljG879al++9BHZxKdt\nKxwjuvE1";

//			byte[] returnByte =Base64Utils.base64ToByteArray(payload.trim());
            byte[] returnByte = base64Decoder.decodeBuffer(payload);
            byte[] decryptByte = EncodeUtil.ees3DecodeECB(key, returnByte);
            String decryptStr = new String(decryptByte, "UTF-8");

            System.out.println("返回数据");
            System.out.println(decryptStr);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {

        }
    }

}
